package com.zpark.dao.impl;

import com.zpark.dao.IUserDao;
import com.zpark.pojo.User;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * @PackageName: com.zpark.dao.impl
 * @ClassName: UserDaoImpl
 * @Description:
 * @author: RZS
 * @date: 2023/4/11  16:55
 */
@Repository
public class UserDaoImpl implements IUserDao {

    private static Map<String, User> users;

    static {
        // 实例化map容器
        users = new HashMap<>();

        // 添加user
        User zs = new User(1, "张三", "123456");
        User ls = new User(2, "李四", "123456");
        User ww = new User(3, "王五", "123456");
        User dc = new User(4, "大锤", "123456");

        // 将user对象添加map集合
        users.put("1", zs);
        users.put("2", ls);
        users.put("3", ww);
        users.put("4", dc);
    }

    @Override
    public List<User> selectAll() {
        List<User> lists = new ArrayList<>();
        // 获取map的所有key
        Set<String> keySet = users.keySet();
        // 循环获取所有key
        for (String key : keySet) {
            // 根据key获取value
            User user = users.get(key);
            // 将user添加到list
            lists.add(user);
        }

        return lists;
    }

    @Override
    public void insertUser(User user) {
        users.put("" + user.getId(), user);
    }
}
